package com.hwpt.chesedemo.presentation.viewmodel

import androidx.lifecycle.ViewModel
import com.hwpt.chesedemo.data.remote.llm.AiService
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject



@HiltViewModel
class TeachingViewModel @Inject constructor(
    val aiService: AiService
) : ViewModel() {

    private val _uiState = MutableStateFlow(TeachingUiState())
    val uiState: StateFlow<TeachingUiState> = _uiState.asStateFlow()

    private val compositeDisposable = CompositeDisposable()

    // 国际象棋常见问题列表
    val chessQuestions = listOf(
        "国际象棋的基本规则是什么？",
        "如何开局比较好？",
        "什么是王车易位？",
        "如何判断棋局胜负？",
        "国际象棋的特殊走法有哪些？",
        "如何提高国际象棋水平？",
        "什么是将军和将死？",
        "国际象棋的棋子价值是多少？"
    )

    fun setShowAIChat(show: Boolean) {
        _uiState.value = _uiState.value.copy(showAIChat = show)
    }

    fun setUserQuestion(question: String) {
        _uiState.value = _uiState.value.copy(userQuestion = question)
    }

    fun setDropdownExpanded(expanded: Boolean) {
        _uiState.value = _uiState.value.copy(isDropdownExpanded = expanded)
    }

    fun sendQuestion() {
        val question = _uiState.value.userQuestion
        if (question.isNotBlank()) {
            _uiState.value = _uiState.value.copy(
                isLoading = true,
                response = "",
                isStreamComplete = false
            )

            val responseBuilder = StringBuilder()

            val disposable: Disposable = aiService.aiTeachingStream(question)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    { chunk: String ->
                        responseBuilder.append(chunk)
                        _uiState.value = _uiState.value.copy(response = responseBuilder.toString())
                    },
                    { error: Throwable ->
                        _uiState.value = _uiState.value.copy(
                            response = "发生错误: ${error.message}",
                            isLoading = false,
                            isStreamComplete = true
                        )
                    },
                    {
                        _uiState.value = _uiState.value.copy(
                            isLoading = false,
                            isStreamComplete = true
                        )
                    }
                )

            compositeDisposable.add(disposable)
        }
    }

    fun resetChat() {
        _uiState.value = _uiState.value.copy(
            response = "",
            userQuestion = "",
            isStreamComplete = false
        )
        compositeDisposable.clear()
    }

    override fun onCleared() {
        super.onCleared()
        compositeDisposable.clear()
    }
}

data class TeachingUiState(
    val showAIChat: Boolean = false,
    val response: String = "",
    val isLoading: Boolean = false,
    val userQuestion: String = "",
    val isDropdownExpanded: Boolean = false,
    val isStreamComplete: Boolean = false
)